package com.aiyige.page.takephoto;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.aiyige.R;
import com.google.android.cameraview.CameraView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TakePhotoPage extends AppCompatActivity {
    private static final String STATE_KEY_CAMERA_FACING = "STATE_KEY_CAMERA_FACING";
    private static final String STATE_KEY_CAMERA_FLASH = "STATE_KEY_CAMERA_FLASH";

    @BindView(R.id.cameraView)
    CameraView cameraView;
    @BindView(R.id.quitBtn)
    ImageView quitBtn;
    @BindView(R.id.switchCameraBtn)
    ImageView switchCameraBtn;
    @BindView(R.id.discardBtn)
    ImageView discardBtn;
    @BindView(R.id.switchflashBtn)
    ImageView switchflashBtn;
    @BindView(R.id.completeBtn)
    ImageView completeBtn;

    boolean shotComplete = false;
    String photoPath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_take_photo);
        ButterKnife.bind(this);
        cameraView.addCallback(new CameraView.Callback() {
            @Override
            public void onCameraOpened(CameraView cameraView) {
                super.onCameraOpened(cameraView);
            }

            @Override
            public void onCameraClosed(CameraView cameraView) {
                super.onCameraClosed(cameraView);
            }

            @Override
            public void onPictureTaken(CameraView cameraView, byte[] data) {
                super.onPictureTaken(cameraView, data);
            }
        });
        if (savedInstanceState == null) {
            cameraView.setFacing(CameraView.FACING_BACK);
            cameraView.setFlash(CameraView.FLASH_OFF);
        } else {
            cameraView.setFacing(savedInstanceState.getInt(STATE_KEY_CAMERA_FACING, CameraView.FACING_BACK));
            cameraView.setFlash(savedInstanceState.getInt(STATE_KEY_CAMERA_FLASH, CameraView.FLASH_OFF));
        }
        updateView();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_KEY_CAMERA_FACING, cameraView.getFacing());
        outState.putInt(STATE_KEY_CAMERA_FLASH, cameraView.getFlash());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        cameraView.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        cameraView.stop();
    }

    @OnClick({R.id.quitBtn, R.id.switchCameraBtn, R.id.discardBtn, R.id.switchflashBtn, R.id.completeBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.quitBtn:
                break;
            case R.id.switchCameraBtn:
                doSwitchCamera();
                break;
            case R.id.discardBtn:
                break;
            case R.id.switchflashBtn:
                doSwitchFlash();
                break;
            case R.id.completeBtn:
                break;
        }
    }

    public void doSwitchCamera() {
        switch (cameraView.getFacing()) {
            case CameraView.FACING_BACK:
                cameraView.setFacing(CameraView.FACING_FRONT);
                break;
            default:
                cameraView.setFacing(CameraView.FACING_BACK);
                break;
        }
        updateView();
    }

    public void doSwitchFlash() {
        switch (cameraView.getFlash()) {
            case CameraView.FLASH_ON:
                cameraView.setFlash(CameraView.FLASH_OFF);
                break;
            default:
                cameraView.setFlash(CameraView.FLASH_ON);
                break;
        }
        updateView();
    }

    public void updateView() {
        switch (cameraView.getFacing()) {
            case CameraView.FACING_BACK:
                switchCameraBtn.setImageResource(R.drawable.shot_camera_right);
                break;
            default:
                switchCameraBtn.setImageResource(R.drawable.shot_camera_left);
                break;
        }
        switch (cameraView.getFlash()) {
            case CameraView.FLASH_ON:
                switchflashBtn.setImageResource(R.drawable.shot_flash);
                break;
            default:
                switchflashBtn.setImageResource(R.drawable.shot_flash_n);
                break;
        }
    }

}
